const joinModel = require('../../model/join/joinModel')
class joinService{
  //获取最新公告
  async getJoin(req,res){
    let {pagenum,per_page} = req.query
    pagenum = parseInt(pagenum)
    per_page = parseInt(per_page)
    if(per_page === 1){
      pagenum = pagenum-1
    }else if(per_page === 2){
      pagenum = (pagenum-1)*2
    }else if(per_page === 5){
      pagenum = (pagenum-1)*5
    }else if(per_page === 10){
      pagenum = (pagenum-1)*10
    }
    console.log(pagenum)
    console.log(per_page)
    const Info = await joinModel.joinInfo()
    const total_count = Info.results.length
    const {results:data} = await joinModel.pageJoin(pagenum,per_page)
    if(data.length != 0){
      return res.json({
        total_count,
        pagenum:pagenum+1,
        per_page,
        data
      })
    }else{
      return res.json({
        meta:{
          msg: '报名信息查询失败',
          status: 404
        }
      })
    }
  }
  //添加报名
  async postAddJoin(req,res){
    let {name,phone,email} = req.body
    console.log(req.body)
    const {results:data} = await joinModel.addJoin(phone,name,email)
    if(data.length != 0){
      return res.json({
        meta:{
          msg: '报名成功',
          status: 200
        }
      })
    }else{
      return res.json({
        meta:{
          msg: '报名失败',
          status: 404
        }
      })
    }
  }
}

module.exports = new joinService()